package com.upay.android.client.handler;

import static com.upay.android.client.helper.Keys.GROUPID;

import java.util.List;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.upay.android.client.R;
import com.upay.android.client.activity.AbstractUPayActivity;
import com.upay.android.client.activity.GroupExactUsersActivity;
import com.upay.android.client.activity.GroupWeightedUsersActivity;
import com.upay.android.client.adapter.GroupArrayAdapter;
import com.upay.domain.group.Group;
import com.upay.domain.response.ResponseEnvelope;

public class GroupSubscribedCallbackHandler extends AbstractCallbackHandler<List<Group>> {
	
	/* ---- Constants ---- */
	
	/* ---- Instance Variables ---- */
	private List<Group> groups = null;
	
	/* ---- Constructors ---- */

	public GroupSubscribedCallbackHandler(AbstractUPayActivity context) {
		this.context = context;
	}
	
	/* ---- Business Methods ---- */
	

	@Override
	public void handleResponse(ResponseEnvelope<List<Group>> response) {
		
		groups = response.getResponseBody().getResponseObject();
		ArrayAdapter<Group> adapter = new GroupArrayAdapter(getContext(), groups);
		ListView groupList = (ListView)getContext().findViewById(R.id.grouplist);
		groupList.setAdapter(adapter);
		groupList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				Group group = groups.get(position);
				Intent intent = null;
				if ("EXACT".equals(group.getGroupPolicy().getId())) {
					intent = new Intent(getContext(), GroupExactUsersActivity.class);
				}
				
				if ("WEIGHTED".equals(group.getGroupPolicy().getId())) {
					intent = new Intent(getContext(), GroupWeightedUsersActivity.class);
				}
				
				intent.putExtra(GROUPID, groups.get(position).getId());
				getContext().startActivity(intent);
			}
		});
	}

	@Override
	public void handleValidationException(ResponseEnvelope<List<Group>> response) {
		// Handle the exception for not providing correct userid and token;
	}

}
